package com.example.myapplication.government;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.ClassificationlistBean;
import com.example.myapplication.ShuJu.GovernmentBean;
import com.example.myapplication.ShuJu.MyappealBean;
import com.example.myapplication.apter.ClassificationApter;
import com.example.myapplication.apter.GovernmentApter;
import com.example.myapplication.apter.MyappealApter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GovernmentMainActivity extends AppCompatActivity {

    private ViewPager re_government;
    private OkHttpClient okHttpClient;
    private Gson gson;
    private Handler handler;
    private Request request;
    private GovernmentBean governmentBean;
    private ClassificationlistBean classificationlistBean;
    private RecyclerView re_centerClassification;
    private MyappealBean myappealBean;
    private RecyclerView re_myappeal;
    private Button  but_appeal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_government_main);
        initview();
        //轮播图
        getCarouselmap();
        //市民诉求分类
        Classificationlist();
        //我的诉求
         Myappeal();
         //点击发布诉求
         sendappeal();
    }

    private void sendappeal() {

    }

    private void Myappeal() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/gov-service-hotline/appeal/list?state=0&pageNum=1&pageSize=10")
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.d("Myappeal","Myappeal失败");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                myappealBean = gson.fromJson(string, MyappealBean.class);
                Log.d("Myappealstring","Myappealstring"+string);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        bingMyappeal();
                    }
                });
            }
        });
    }

    private void bingMyappeal() {
        MyappealApter myappealApter =   new MyappealApter(GovernmentMainActivity.this,myappealBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(GovernmentMainActivity.this);
        re_myappeal.setAdapter(myappealApter);
        re_myappeal.setLayoutManager(linearLayoutManager);
    }

    private void Classificationlist() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/gov-service-hotline/appeal-category/list")
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                classificationlistBean = gson.fromJson(string, ClassificationlistBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Classification();
                    }
                });
            }
        });
    }

    private void Classification() {
        ClassificationApter classificationApter =   new ClassificationApter(GovernmentMainActivity.this,classificationlistBean.getRows());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(GovernmentMainActivity.this,4);
        //gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
        re_centerClassification.setAdapter(classificationApter);
        re_centerClassification.setLayoutManager(gridLayoutManager);
    }

    private void getCarouselmap() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/gov-service-hotline/ad-banner/list")
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                governmentBean = gson.fromJson(string, GovernmentBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        sendviewpage();
                    }
                });
            }
        });
    }

    private void sendviewpage() {
        GovernmentApter governmentApter = new GovernmentApter(GovernmentMainActivity.this,governmentBean.getData());
        re_government.setAdapter(governmentApter);
        mHandler.sendEmptyMessageDelayed(0, 3 * 500);
    }

    private void initview() {
        re_government = findViewById(R.id.re_government);
        re_centerClassification = findViewById(R.id.re_centerClassification);
        but_appeal = findViewById(R.id.but_appeal);
        re_myappeal = findViewById(R.id.re_myappeal);
        okHttpClient = new OkHttpClient();
        gson = new Gson();
        handler = new Handler();
    }


    private  Handler mHandler   = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                int index = re_government.getCurrentItem();
                index = (index + 1) % 5;
                re_government.setCurrentItem(index);
                mHandler.sendEmptyMessageDelayed(0, 3 * 500);
            }
        }
    };

}